﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using OrderManagementSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderManagementSystem.Tests
{
    class OrderServiceTests
    {
        static void Main(string[] args)
        {
            TestAddOrder();
            TestRemoveOrder();
            TestModifyOrder();
            TestQueryOrders();
            TestSortOrders();
            TestExportAndImport();
        }

        static void TestAddOrder()
        {
            OrderService orderService = new OrderService();
            Order order = new Order { OrderId = 1, Customer = "Alice", Details = new List<OrderDetail> { new OrderDetail { ProductName = "Product1", Price = 10, Quantity = 2 } } };

            // 添加订单
            orderService.AddOrder(order);
            List<Order> orders = orderService.QueryOrders(o => o.Customer == "Alice");

            // 验证订单是否成功添加
            if (orders.Count == 1 && orders[0].OrderId == 1)
            {
                Console.WriteLine("AddOrder test passed.");
            }
            else
            {
                Console.WriteLine("AddOrder test failed.");
            }
        }

        static void TestRemoveOrder()
        {
            OrderService orderService = new OrderService();
            Order order1 = new Order { OrderId = 1, Customer = "Alice", Details = new List<OrderDetail> { new OrderDetail { ProductName = "Product1", Price = 10, Quantity = 2 } } };
            orderService.AddOrder(order1);

            // 移除订单
            orderService.RemoveOrder(1);
            List<Order> orders = orderService.QueryOrders(o => true);

            // 验证订单是否成功移除
            if (orders.Count == 0)
            {
                Console.WriteLine("RemoveOrder test passed.");
            }
            else
            {
                Console.WriteLine("RemoveOrder test failed.");
            }
        }

        static void TestModifyOrder()
        {
            OrderService orderService = new OrderService();
            Order order1 = new Order { OrderId = 1, Customer = "Alice", Details = new List<OrderDetail> { new OrderDetail { ProductName = "Product1", Price = 10, Quantity = 2 } } };
            orderService.AddOrder(order1);

            // 修改订单
            Order newOrder = new Order { OrderId = 1, Customer = "Alice", Details = new List<OrderDetail> { new OrderDetail { ProductName = "Product3", Price = 30, Quantity = 3 } } };
            orderService.ModifyOrder(1, newOrder);
            List<Order> orders = orderService.QueryOrders(o => o.Customer == "Alice");

            // 验证订单是否成功修改
            if (orders.Count == 1 && orders[0].Details.Count == 1 && orders[0].Details[0].ProductName == "Product3")
            {
                Console.WriteLine("ModifyOrder test passed.");
            }
            else
            {
                Console.WriteLine("ModifyOrder test failed.");
            }
        }

        static void TestQueryOrders()
        {
            OrderService orderService = new OrderService();
            Order order1 = new Order { OrderId = 1, Customer = "Alice", Details = new List<OrderDetail> { new OrderDetail { ProductName = "Product1", Price = 10, Quantity = 2 } } };
            Order order2 = new Order { OrderId = 2, Customer = "Bob", Details = new List<OrderDetail> { new OrderDetail { ProductName = "Product2", Price = 20, Quantity = 1 } } };
            orderService.AddOrder(order1);
            orderService.AddOrder(order2);

            // 查询订单
            List<Order> orders = orderService.QueryOrders(o => o.Customer == "Alice");

            // 验证查询结果
            if (orders.Count == 1 && orders[0].OrderId == 1)
            {
                Console.WriteLine("QueryOrders test passed.");
            }
            else
            {
                Console.WriteLine("QueryOrders test failed.");
            }
        }

        static void TestSortOrders()
        {
            OrderService orderService = new OrderService();
            Order order1 = new Order { OrderId = 2, Customer = "Bob", Details = new List<OrderDetail> { new OrderDetail { ProductName = "Product2", Price = 20, Quantity = 1 } } };
            Order order2 = new Order { OrderId = 1, Customer = "Alice", Details = new List<OrderDetail> { new OrderDetail { ProductName = "Product1", Price = 10, Quantity = 2 } } };
            orderService.AddOrder(order1);
            orderService.AddOrder(order2);

            // 排序订单
            orderService.SortOrders();
            List<Order> orders = orderService.QueryOrders(o => true);

            // 验证排序结果
            if (orders.Count == 2 && orders[0].OrderId == 1 && orders[1].OrderId == 2)
            {
                Console.WriteLine("SortOrders test passed.");
            }
            else
            {
                Console.WriteLine("SortOrders test failed.");
            }
        }

        static void TestExportAndImport()
        {
            OrderService orderService = new OrderService();
            Order order1 = new Order { OrderId = 1, Customer = "Alice", Details = new List<OrderDetail> { new OrderDetail { ProductName = "Product1", Price = 10, Quantity = 2 } } };
            Order order2 = new Order { OrderId = 2, Customer = "Bob", Details = new List<OrderDetail> { new OrderDetail { ProductName = "Product2", Price = 20, Quantity = 1 } } };
            orderService.AddOrder(order1);
            orderService.AddOrder(order2);

            // 导出订单
            orderService.Export("test_orders.xml");

            // 清空订单列表
            orderService = new OrderService();

            // 导入订单
            orderService.Import("test_orders.xml");
            List<Order> orders = orderService.QueryOrders(o => true);

            // 验证导入结果
            if (orders.Count == 2)
            {
                Console.WriteLine("ExportAndImport test passed.");
            }
            else
            {
                Console.WriteLine("ExportAndImport test failed.");
            }
        }
    }

}